package com.fishleap.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * @author zlg
 * @create 2020-05-23 10:27
 */
@Configuration
public class MyLocaleResolver implements LocaleResolver {

    // 自定义 区域解析方式
    @Override
    public Locale resolveLocale(HttpServletRequest httpServletRequest) {
        // 获取页面手动传递的语言参数值l  : zh_CN  en_US  ''
        String l = httpServletRequest.getParameter("l");
        Locale locale = null;
        if (!StringUtils.isEmpty(l)) {
            // 如果参数不为空，就根据参数值进行手动语言切换
            String[] strs = l.split("_");
            locale = new Locale(strs[0], strs[1]);
        }else {
            //Accept-Language: zh-CN ,zh;q=0.9
            String header = httpServletRequest.getHeader("Accept-Language");
            String[] split = header.split(","); // zh-CN
            String[] strs = split[0].split("-");
            locale = new Locale(strs[0], strs[1]);  // zh  CN
        }
        return locale;
    }


    @Override
    public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {
    }


    // 将自定义的MyLocaleResovle重新注册成一个类型为LocaleResolver的bean组件
    @Bean
    public LocaleResolver localeResolver() {
        return new MyLocaleResolver();
    }

}
